import {Component} from '@angular/core';
import {App, IonicPage, NavController} from 'ionic-angular';
import {PublicService} from "../../common/public.service";
import {HttpService} from "../../providers/HttpService";
import {DatePicker} from "@ionic-native/date-picker";
import {ATTEN_REPORT_URL} from "../../providers/Constants";

/**
 * 在该页面班长可以查询自己管辖工序的某日某个班组的出勤情况。
 * （系统自动记录员工最后一次打卡的工序，默认员工属于该工序）
 */

@IonicPage()
@Component({
  selector: 'page-work-report',
  templateUrl: 'work-report.html',
})
export class WorkReportPage {


  bzDate: string;//班组日期
  bzDate_old: string;//监控日期是否变化
  bzSelect: string;//班组选择
  kqType: string = "";

  //处理类型
  public attendanceTypes: Array<Object> = [
    {"code": "01", "name": "旷工"},
    {"code": "02", "name": "请假"},
    {"code": "03", "name": "公休"},
    {"code": "00", "name": "正常"}];

  //扫描后把数据保存在这里
  public dataList: Array<Object> = [];

  constructor(public navCtrl: NavController,
              public pub: PublicService,
              public app: App,
              public datePicker: DatePicker,
              public http: HttpService) {
  }

  ionViewDidLoad() {
    this.bzDate = sessionStorage.getItem("setWorkTime");
    if (!this.bzDate) {
      this.bzDate = this.pub.getDate(new Date(), 0);
    }
    this.bzSelect = sessionStorage.getItem("setBzSelect");
    this.bzDate_old = this.bzDate;
  }

  ionViewWillEnter() {
    this.initList();
  }


  /**
   * 设置班组日期
   */
  getDate() {
    this.datePicker.show({
      titleText: "",
      date: this.bzDate,
      mode: 'date',
      maxDate: new Date(),
      is24Hour: true,
      androidTheme: this.datePicker.ANDROID_THEMES.THEME_HOLO_LIGHT
    }).then(
      date => {
        this.bzDate = this.pub.getDate(date, 0);
        if (this.bzDate != this.bzDate_old) {
          this.bzDate_old = this.bzDate;
          this.changeQuery();
        }
      },
      err => {
        console.log('Error occurred while getting date: ', err);
      }
    );
  }

  initList() {
    this.dataList = [];
    let params = {
      sessionId: sessionStorage.getItem("session"),
      fdDate: this.bzDate,
      fdType: this.bzSelect,
      fdKqType: this.kqType
    };
    this.http.get(SERVER_URL + ATTEN_REPORT_URL, '&', params, false).then(res => {
      if (res.success) {
        this.dataList = res.data.attenList;
      } else {
        this.pub.presentLoadingToast(res.msg);
        if (res.timeout) {
          setTimeout(() => {
            this.app.getRootNav().setRoot('LoginPage');
          }, 1500);
        }
      }
    });
  }


  /**
   *
   * 切换查询条件
   */
  changeQuery() {
    this.initList();
  }
}
